package de.tuhrig.sportlog.pmf.queries;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import de.tuhrig.sportlog.user.log.Log;

public class QueryFactory {

	private PersistenceManager pm;

	public QueryFactory(PersistenceManager pm) {
		
		this.pm = pm;
	}
	
	public Query countAllLogs() {
		
		Query query = pm.newQuery(Log.class);
        query.setResult("count(this)");
        
        return query;
	}
	
	public Query getAllLogs() {
		
		Query query = pm.newQuery(Log.class);
		query.setOrdering("key desc");
        
        return query;
	}
	
	public Query getLogs(int from, int to) {
		
		Query query = pm.newQuery(Log.class);
		query.setOrdering("key desc");
		query.setRange(from, to);
		
        return query;
	}
	
	public Query countAllLogsFromUserId() {
		
        Query query = pm.newQuery(Log.class);
        query.setFilter("userId == userIdParam");
        query.declareParameters("String userIdParam");
        query.setResult("count(this)");
        
        return query;
	}
	
	public Query getAllLogsFromUserId() {
		
        Query query = pm.newQuery(Log.class);
        query.setFilter("userId == userIdParam");
        query.declareParameters("String userIdParam");
        query.setOrdering("key desc");
        
        return query;
	}

	public Query getLogsFromUserId(int from, int to) {
		
        Query query = pm.newQuery(Log.class);
        query.setFilter("userId == userIdParam");
        query.declareParameters("String userIdParam");
        query.setOrdering("key desc");
    	query.setRange(from, to);
    	
        return query;
	}
	
	public Query getLogsFromUserIdAfterDate() {
		
        Query query = pm.newQuery(Log.class);
        query.setFilter("userId == userIdParam && date >= dateParam");
        query.declareParameters("String userIdParam, String dateParam");
    	
        return query;
	}
	
	public Query getLogsFromUserIdFromDate(int from, int to) {
		
        Query query = pm.newQuery(Log.class);
        query.setFilter("userId == userIdParam && date >= dateParam1 && date < dateParam2");
        query.declareParameters("String userIdParam, String dateParam1, String dateParam2");
        query.setOrdering("date desc");
    	query.setRange(from, to);
        
        return query;
	}
}